﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SanctusSoftware.Music.Chords;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;

using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

using SanctusSoftware.Music.ChordSheetEditor.Client;
using SanctusSoftware.Music.ChordSheetEditor.Wpf;
using SanctusSoftware.Music.ChordSheetEditor.Word;

namespace Chord_Sheet_Add_in_for_Word_2007
{
  public partial class ThisAddIn : IWordChordSheetEditorHostView
  {

    //private TransposeOptions transposeOptions = null;
    //private Settings settings = null;

    private WordChordSheetEditorPresenter _Presenter = null;

    #region Startup/Shutdown

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      _Presenter = new WordChordSheetEditorPresenter(this, Globals.Ribbons.ChordSheetRibbon);
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #endregion

    #region IWordChordSheetEditorHostView

    public Document CurrentDocument
    {
      get
      {
        return Application.ActiveDocument;
      }
      set
      {
        // TODO: should be able to loop through the various documents and activate the correct one
        throw new NotImplementedException();
      }
    }

    public Document GetNewDocument()
    {
      return Application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    }

    public Selection Selection
    {
      get { return Application.Selection; }
    }

    #endregion

    #region IChordSheetEditorHostView

    public ISentence[] Sentences
    {
      get 
      {
        if (this.CurrentDocument == null || this.CurrentDocument.Sentences.Count == 0)
        {
          return null;
        }
        else
        {

          // Watch out for Vertical tabs (CTRL+ENTER)
          var softSentences = SoftSentenceHelper.GetSoftSentences(this.CurrentDocument.Sentences);

          ISentence[] retArray = new ISentence[softSentences.Count];
          for (int i = 0; i < softSentences.Count; i++)
          {
            retArray[i] = new WordSentence(softSentences[i]);
          }
          return retArray;
        }
      }
    }

    public string Status
    {
      get
      {
        return "";
      }
      set
      {
        Application.StatusBar = value;
      }
    }

    public bool AllowScreenUpdating
    {
      get
      {
        return Application.ScreenUpdating;
      }
      set
      {
        Application.ScreenUpdating = value;
      }
    }

    public void ShowMessage(string message)
    {
      MessageBox.Show(message);
    }


    public bool PromptRemoveHyperlinksFromChords()
    {
      ChordsWithHyperlinksErrorWindow window = new ChordsWithHyperlinksErrorWindow();
      ElementHost.EnableModelessKeyboardInterop(window);
      return (window.ShowDialog() == true);
    }


    public int? PromptMinimumChordLineMatchPercentage(int current)
    {
      TransposeOptionsWindow window = new TransposeOptionsWindow();
      ElementHost.EnableModelessKeyboardInterop(window);
      window.MinimumChordLineMatchPercentage = current;
      if (window.ShowDialog() == true)
      {
        return window.MinimumChordLineMatchPercentage;
      }
      else
      {
        return null;
      }

    }

    public IRichTextFormat GetTextFormat(IRichTextFormat current)
    {

      FormatWindow window = new FormatWindow();
      window.AvailableColors = RichTextFormatColorHelper.AvailableColors;
      ElementHost.EnableModelessKeyboardInterop(window);

      if (current != null)
      {
        window.FontNameValue = current.FontNameValue;
        window.FontSizeValue = current.FontSizeValue;
        window.FontColorValue = current.FontColorValue;
        window.BoldValue = current.BoldValue;
        window.ItalicValue = current.ItalicValue;
        window.UnderlineValue = current.UnderlineValue;
      }

      if (window.ShowDialog() == true)
      {
        return window;
      }
      else
      {
        return null;
      }

    }

    public void SelectRange(ISentence start, ISentence end)
    {
      this.Selection.Start = ((Range)start.Tag).Start;
      this.Selection.End = ((Range)end.Tag).End;
    }

    #endregion

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
      this.Startup += new System.EventHandler(ThisAddIn_Startup);
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion

  }
}



